package com.hp.TrimServiceSamples;

import com.hp.TrimClient.Enums.*;
import com.hp.TrimClient.*;
import com.hp.TrimClient.Enums.BaseObjectTypes;
import com.hp.TrimClient.Enums.DownloadType;
import com.hp.TrimClient.Enums.LocationType;
import com.hp.TrimClient.Enums.PropertyIds;
import com.hp.TrimClient.Enums.PropertyType;
import com.hp.TrimClient.Enums.StringDisplayType;
import com.hp.TrimClient.Enums.UserOptionSetIds;
import com.hp.TrimClient.MainObjectMethodModels.SetUserLabel;
import com.hp.TrimClient.MainObjectMethodModels.RemoveUserLabel;
import com.hp.TrimClient.MainObjects.RecordType;
import com.hp.TrimClient.TrimDateTime;
import com.hp.TrimClient.TrimUserOptionsSet;
import com.hp.TrimClient.UserOptions.DropzoneUserOptions;
import com.hp.TrimClient.UserOptions.SearchUserOptions;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;

import com.hp.TrimClient.MainObjects.Record;
import com.hp.TrimClient.MainObjects.Location;
import com.hp.TrimClient.MainObjectCollections.Records;
import com.hp.TrimClient.MainObjects.UserLabel;
import com.hp.TrimClient.MainObjectCollections.RecordsResponse;
import com.hp.TrimClient.MainObjectCollections.LocationsResponse;

import com.hp.TrimClient.MainObjectMethodModels.DeleteMainObject;

import com.hp.TrimClient.Models.RecordDownload;

import java.io.File;
import java.lang.Exception;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;

public class TrimSample {

	private static TrimClient trimClient;




	private static void fetchUserOptions() throws Exception {
		TrimUserOptions request = new TrimUserOptions();
		request.setUserOptionSetId(UserOptionSetIds.Search);

		TrimUserOptionsResponse response = trimClient.get(request);

		System.out.println(((SearchUserOptions)response.getUserOptions()).getRecordEditor());

	}

	private static void updateUserOptions() throws Exception {

		DropzoneUserOptions dropzoneOptions = new DropzoneUserOptions();
		dropzoneOptions.setOnMinimise(true);
		dropzoneOptions.setShowClassifications(true);

		TrimUserOptionsResponse response = trimClient.post(dropzoneOptions);

		System.out.println(((DropzoneUserOptions)response.getUserOptions()).getOnMinimise());
	}

	private static void createNewRecord() throws Exception {

		RecordType recordType = new RecordType();
		recordType.setUri(2);

		Record record = new Record(trimClient);
		record.setRecordType(recordType);
		record.setTitle("My test Java Record");

		record.post();

		System.out.println(record.getTitle());
	}

	private static void createNewLocation() throws Exception {
		Location newLocation = new Location(trimClient);
		newLocation.setTypeOfLocation(LocationType.Person);
		newLocation.setGivenNames("Bob");
		newLocation.setSurname("Java Man");
		newLocation.addFetchProperties(PropertyIds.LocationSortName);

		newLocation.post();

		System.out.println(newLocation.getSortName().getValue());
	}

	private static void updateRecord() throws Exception {
		Record record = new Record(trimClient);
		record.setUri(9000000336L);
		record.setTitle("A new Title");

		record.post();
		System.out.println(record.getTitle());
	}

	private static void deleteRecord() throws Exception {
		DeleteMainObject deleteMainObject = new DeleteMainObject();
		deleteMainObject.setId("9000000337");
		deleteMainObject.setTrimType(BaseObjectTypes.Record);
		trimClient.post(deleteMainObject);
	}

	private static void applyUserLabel() throws Exception {
		Record record = new Record(trimClient);
		record.setUri(9000000340L);

		record.addAction(new SetUserLabel());
		SetUserLabel lbl = new SetUserLabel();

		UserLabel newLabel = new UserLabel(9000000008L);

		lbl.setUserLabelToApply(newLabel);

		record.addAction(lbl);

		record.addFetchProperties("IsInFavorites", PropertyIds.AttachedLabels);

		record.post();

		System.out.println(record.getIsInFavorites());
		System.out.println(record.getAttachedLabels().getValue());
	}

	private static void uploadDocument() throws Exception {
		Record record = new Record(trimClient);
		record.addFetchProperties(PropertyIds.RecordExtension, PropertyIds.RecordTitle);
		record.setRecordType(new RecordType(2));

		File file = new File((String)null, "mini_stream.jpeg");
		record.post(file);

		System.out.println(record.getExtension());
	}

	private static void updateDocument() throws Exception {
		Record record = new Record(9000000343L, trimClient);
		record.addFetchProperties(PropertyIds.RecordExtension, PropertyIds.RecordTitle);


		File file = new File((String)null, "moonshot.jpeg");
		record.post(file);

		System.out.println(record.getExtension());
	}

	private static void uploadFromServer() throws Exception {
		Record record = new Record(trimClient);
		record.setRecordType(new RecordType(2));
		record.setFilePath("slate.jpg");

		record.addFetchProperties(PropertyIds.RecordExtension, PropertyIds.RecordTitle);

		record.post();

		System.out.println(record.getExtension());
	}


	private static void downloadDocument() throws Exception {
		RecordDownload request = new RecordDownload();
		request.setId("9000000346");
		request.setDownloadType(DownloadType.Document);

		String filePath = trimClient.getDocument(request, null);

		System.out.println(filePath);
	}

	private static void setCustomFields() throws Exception {
		Record record = new Record(trimClient);
		record.setUri(9000000346l);

		record.setCustomField("PlaceOfInfringement", "Wagga Wagga");
		record.setCustomField("PoliceOfficer", new Location(1));

		Calendar c = Calendar.getInstance();
		c.set(1978, 8, 22);

		record.setCustomField("DateOfBirth", c.getTime());

		record.post();

		System.out.println(record.getFieldValue("PlaceOfInfringement"));
	}

	private static void setCustomFieldAndFileTest() throws Exception {
		Record record = new Record(trimClient);
	//	record.setUri(9000000346l);

		record.setRecordType(new RecordType(2));
		record.setTitle("test field from Java");


		record.setCustomField("PlaceOfInfringement", "Wagga Wagga");


		File file = new File((String)null, "mini_stream.jpeg");
		record.post(file);


		System.out.println(record.getFieldValue("DCTest"));
	}

	private static void getCustomFields() throws Exception {
		Records request = new Records();
		request.setQ("uri:9000000346");

		request.addFetchProperties("PlaceOfInfringement", "PoliceOfficer", "DateOfBirth");
		request.setPropertyValue(PropertyType.Both);
		request.setStringDisplayType(StringDisplayType.ViewPane);

		RecordsResponse response = trimClient.get(request);

		for (Record record : response.getResults()) {
			System.out.println(	record.getFieldValueAsString("PlaceOfInfringement"));
			System.out.println(record.getFieldValue("PlaceOfInfringement"));


			System.out.println(	record.getFieldValueAsString("PoliceOfficer"));
			System.out.println(((Location)record.getFieldValue("PoliceOfficer")).getUri());

			System.out.println(	record.getFieldValueAsString("DateOfBirth"));

			Calendar c = Calendar.getInstance();
			c.setTime(((TrimDateTime) record.getFieldValue("DateOfBirth")).getDateTime());

			System.out.println(c.get(Calendar.YEAR));
		}
	}

	private static void recordSearch() throws Exception {
		Records request = new Records();
		request.setQ("all");

		request.addFetchProperties(PropertyIds.RecordTitle);

		RecordsResponse response = trimClient.get(request);

		for (Record record : response.getResults()) {
			System.out.println(	record.getTitle().getValue());
		}
	}

	public static void main(String[] args) throws Exception {
		
		//trimClient = new TrimClient("http://xxx.xxx.xxx.xxx/ServiceAPI",
		//									   new UsernamePasswordCredentials("itu_tadmin", "XXXXXXX"));



		if (args == null || args.length < 3) {
			System.out.println("Three parameters are required, ServiceAPI URL, user name and password.");

			return;
		}

		trimClient = new TrimClient(args[0],
				new NTCredentials(args[1], args[2], null, null));

		if (args != null && args.length > 3) {
			//try {
				Method method = TrimSample.class.getDeclaredMethod(args[3]);
				method.setAccessible(true);

				method.invoke(null);

				return;

		} else {
			System.out.println("No method specified, calling recordSearch.");


			recordSearch();
		}
	//	fetchUserOptions();

	}
}